/**
 * 主功能页面，用于包括所有页面元素并加载显示其它页面部分
 *    主要还用于解析公共数据，用于其它页面数据准备工作
 * author:zhushy
 * date:2016年8月22日22:30:42
 */

var kmMain = {
	/**
	 * bindEvent 页面事件
	 */
	bindEvent: function () {
		
		$("#rtn_industryEquip").unbind().on("click", function () {
			//返回
			 window.location.href = "#selectIndustry";
        });
		
		$(".next-operation").unbind().on("click", function () {
			summer.closeWin();
		});
		//跳转搜索
		$("#indexPageSearchInput").unbind().on("click", function () {
			window.sessionStorage.setItem("clear_input", true);
			window.sessionStorage.removeItem("searchKey");
			//显示返回键
			window.sessionStorage.searchKeyNotBackFlag = "true";
			//标题的显示
			window.sessionStorage.pageTitle = "行业图表"; 
			//记录跳转之前的页面路由，用于返回
            var currentURL = window.location.href;
            window.sessionStorage.productListPageFromRout = currentURL.split("#")[1];
			window.location.href = "#productList/industry";
		});
	},
	/**
	 * 
	 */
	loadData: function (res) {
		//显示数据
		kmMain.showUserSetting(res.data);
		// 从内存去最近搜索
		var localStorageGetKeywords = window.localStorage.getItem("keywords");
		var keywordArray = JSON.parse(localStorageGetKeywords);
		//没有搜索 隐藏
		if (!keywordArray) {
			$("#search-resent").hide();
		} else {
			$("#search-resent").show();
			//倒序
			keywordArray.reverse();
			var html = '<div class="knowledge-base-count" id="search-resent"><div class="set-list-h1">最近搜索</div><div class="knowledge-second">';
			 var randomNum = Math.round(Math.random()*3+1);
			for (var i = 0; i < keywordArray.length; i++) {
				//空字符串跳过
				if (keywordArray[i].length == 0) {
					continue;
				} else if (industryEquip.DataLength(keywordArray[i]) > 16) {
					keywordArray[i] = keywordArray[i].substring(0,16) + "...";
				}
				 //生成1-4的随机整数
				var showIndex = Math.round(Math.random()*3+1);
				while(showIndex == randomNum){
					showIndex = Math.round(Math.random()*3+1);
					if(showIndex != randomNum){
						randomNum = showIndex;
						break;
					}
				};
				randomNum = showIndex;
				html += '<div class="knowledge-search-item konwledge-'+randomNum+'">' + keywordArray[i] + '</div>';
			}
			html += '</div>';
			$("#knowledge-base-list").append(html);
			//跳转搜索结果
			$(".knowledge-search-item").unbind().on("click", function (e) {
				var key = e.currentTarget.innerText;
				window.sessionStorage.searchKey = key;
				window.sessionStorage.setItem("clear_input", false);
				//显示返回键
				window.sessionStorage.searchKeyNotBackFlag = "true";
				//标题的显示
				window.sessionStorage.pageTitle = "搜索";
				window.location.href = "#productList/type1";
			});
		}
		//初始化，isocroll
		common.initIscrollBody("knowledge-base-listmain");
	},
	/**
	 * 用于显示用户设置
	 */
	showUserSetting: function (data) {
		//拼接显示内容
		var html = '<div class="knowledge-base-count">';
		for (var i = 0; i < data.length; i++) {
			var industry = data[i];
			// if (industry.list.length > 0) {
				html += '<div class="set-list-h1"><span class="set-list-span"></span>' + industry.cate_name + '<div class="list-h1-icon"></div></div>';
				for (var j = 0; j < industry.list.length; j++) {
					var step = industry.list[j];
					if (step.list.length > 0) {
						html += '<div class="set-list-h2-bg"></div><div class="set-list-h2"><div class="set-list-h3">' + step.cate_name + '<i class="triangle"><img src="images/triangle.png"/></i></div><div class="knowledge-second">'
						var randomNum = Math.round(Math.random()*3+1);
						for (var k = 0; k < step.list.length; k++) {
							var equip = step.list[k];
							//生成1-4的随机整数
							var showIndex = Math.round(Math.random()*3+1);
							while(showIndex == randomNum){
								showIndex = Math.round(Math.random()*3+1);
								if(showIndex != randomNum){
									randomNum = showIndex;
									break;
								}
							};
							randomNum = showIndex;
							////console.log(equip);
							html += '<div class="knowledge-item konwledge-'+randomNum+'" id="' + equip.pk_industry_chart + '" industry_name= "' + equip.cate_name + '">' + equip.cate_name + '</div>';
						};
						html += '</div>';
					};
					// html +='<div class="knowledge-item" id="'+data[i].list[j].pk_industry_chart+'" industry_name= "'+data[i].cate_name+'">'+data[i].cateList[j].cate_name+'</div>';
				}
				html += '</div><div class="margin-gray"></div>';
			//};
		}
		html += '</div>';
		//显示
		$("#knowledge-base-list").html(html);
		//绑定事件
		$(".set-list-h1").unbind().on("click", function (){
			var text = $(this).text();
			window.sessionStorage.searchKey = text;
			//标题的显示
			window.sessionStorage.pageTitle = "行业图表";
			//记录跳转之前的页面路由，用于返回
            var currentURL = window.location.href;
            window.sessionStorage.productListPageFromRout = currentURL.split("#")[1];
			window.location.href = "#productList/industry";
		});
		$(".set-list-h3").unbind().on("click", function (){
			var text = $(this).text();
			window.sessionStorage.searchKey = text;
    				// window.location.href = "#productList/type1";
			//标题的显示
			window.sessionStorage.pageTitle = "行业图表";
			//记录跳转之前的页面路由，用于返回
            var currentURL = window.location.href;
            window.sessionStorage.productListPageFromRout = currentURL.split("#")[1];
    		window.location.href = "#productList/industry";
		});
		//跳转知识导航
		$(".knowledge-item").unbind().on("click", function (e) {
			//跳转
			var pk_industry_chart = e.currentTarget.id;
			var industry_name = e.currentTarget.attributes.industry_name.nodeValue;
			var innerText = e.currentTarget.innerText;
			pk_industry_chart = pk_industry_chart + "," + innerText;
			window.sessionStorage.searchKey = industry_name;
			window.sessionStorage.pk_industry_chart = pk_industry_chart;
			window.location.href = "#competList";
		});

	}
};
var industryEquip = {
	/**
	 * 进行页面路由监听配置
	 * 
	 */
	bindRout: function () {
		//默认空时候进入首页（行业选择界面）
		Path.map("#selectIndustry").to(function () {
			//记录状态
			common.loadPageDiv("html/selectIndustry.html", false,true);
		});

		Path.map("#competList").to(function () {
			//记录状态
			common.loadPageDiv("html/competList.html", true);
		});
		
		//查询结果页面
		Path.map("#productList").to(function () {
			common.loadPageDiv("html/productList.html", true);
		});
		//查询结果列表点解进入页面
		Path.map("#productList/:status").to(function () {
			var type = this.params['status'];
			window.sessionStorage.searchType = type;
			common.loadPageDiv("html/productList.html", true);
		});
		
		Path.listen();
	},
	/**
	*
	*/
	init: function () {
		//调用根据用户查询所有行业信息-带是否设置状态，判断是否已经设置
		var param = {
			"cate_name": window.sessionStorage.selectedIndustryName
		};
		//转圈
		//common.showLoading();
		var url = "/kmsetting/App/queryHyGxSbInfo";
		common.interfaceAjaxFunction(url, param, industryEquip.queryUserSettingSuccess, true);
    },
    /**
     * 根据当前用户，判断是否设置好行业，然后跳入不同页面
     */
    queryUserSettingSuccess: function (json) {
		var _this = this;
		//成功时候
		if (json.status == "0") {
			if (json.data != undefined && json.data.length > 0) {
				//加载设置后的主页面
				kmMain.loadData(json);
			} else {
				//加载设置页面
				//给出信息提示
				$("#knowledge-base-list").height($(window).height() - $(".header").height() - $("#indexPageSearchInput").height());
				common.show_nodata("当前行业无工序设备信息","knowledge-base-list");
			}
		} else {
			//给出信息提示
			$("#knowledge-base-list").height($(window).height() - $(".header").height() - $("#indexPageSearchInput").height());
			common.show_error(json.message,"knowledge-base-list");
		}
		//转圈提示去除
		//common.hideLoading();
    },
	/**
         * 根据传入的字符串计算出长度
         * @param str 需要进行计算长度的字符串
         */
    DataLength: function (str) {
		var len = 0;
		if (!str) {
			len = 0;
		}else{
			if (str.match(/[^ -~]/g) == null) {
				len = str.length;
			}
			else {
				len = str.length + str.match(/[^ -~]/g).length;
			}
		}
		return len;
	},
    /**
     * each page
     */
    initialize: function () {
    	//开始转圈
		//common.showLoading();
		//附件长度
    	var data =  common.strToJson(window.localStorage.currentUserAttachDownloadFiles);
    	$(".enclosure-number").html(typeof(data) == "undefined"? "0" : data.length);
		//事件绑定
		kmMain.bindEvent();
		//路由绑定
		industryEquip.bindRout();
		//加载第一个页面
		industryEquip.init();
		//定义页面列表高度
		$("#knowledge-base-list").css("min-height",$(window).height() - $("#header").height() - $(".knowledge-base-search").height());
    }
};